package IMIC.NGUYENVANTINH.ARRAY.EXCERCISE.EX05;

public class Models {

	public int[] BubbleSort(int[] Array) {
		int temp, counter, index;

		for (counter = 0; counter < Array.length - 1; counter++) { // Loop once
																	// for each
																	// element
																	// in the
																	// array.
			for (index = 0; index < Array.length - 1 - counter; index++) { // Once
																			// for
																			// each
																			// element,
																			// minus
																			// the
																			// counter.
				if (Array[index] > Array[index + 1]) { // Test if need a swap or
														// not.
					temp = Array[index]; // These three lines just swap the two
											// elements:
					Array[index] = Array[index + 1];
					Array[index + 1] = temp;
				}
			}

		}
		return Array;
	}

	public int[] SelectSort(int[] arr) {
		for (int i = arr.length - 1; i >= 1; i--) {
			// Find the maximum in the list[0..i]
			int currentMax = arr[0];
			int currentMaxIndex = 0;
			{
				for (int j = 1; j <= i; j++) {
					if (currentMax < arr[j]) {
						currentMax = arr[j];
						currentMaxIndex = j;
					}
				}
				// Swap list[i] with list[currentMaxIndex] if necessary;
				if (currentMaxIndex != i) {
					arr[currentMaxIndex] = arr[i];
					arr[i] = currentMax;
				}
			}

		}
		return arr;
	}

	public int[] BubbleSorts(int[] myarr) {
		// Tao vong lap for bat dau tu 0..n-1;
		for (int i = 0; i < myarr.length - 1; i++) {
			// Vong lap for tu 0..n-1-i;
			for (int j = 0; j < myarr.length - 1 - i; j++) {
				if (myarr[j] > myarr[j + 1]) {
					int temp = myarr[j];
					myarr[j] = myarr[j + 1];
					myarr[j + 1] = temp;
				}
			}
		}
		return myarr;

	}
//{7,99, 2, 4, 5, 10, 100, 8, -22, 44, 3, 60};
	public int[] SapXepNoiBot(int[] myarr) {
//		{99, 2, 4, 5, 10, 100, 8, -22, 44, 3, 60};
		for (int i = 0; i < myarr.length - 1; i++) {
			for (int j = 0; j < myarr.length - 1-i; j++) {
				if (myarr[j] > myarr[j + 1]) {
					int temp = myarr[j];
					myarr[j] = myarr[j + 1];
					myarr[j + 1] = temp;
				}
			}
		}

		return myarr;
	}
	public int[] Sort(int[] arr){
		for(int i=0;i<arr.length-1;i++){
		     for(int j=i+1;j<= arr.length-1;j++){
			  if(arr[i] > arr[j]){
			  	int temp = arr[i];
				arr[i] = arr[j];
				arr[j] = temp;
			          }
		              }
		 }

		return arr;
	}

}
